package com.powernode.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.io.IOException;

/**
 * 验证码控制器
 */
@Controller
public class CaptchaController {

    @GetMapping("/code/image")
    public void code(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CircleCaptcha code = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
        //将验证码保存到session中
        request.getSession().setAttribute("code", code.getCode());
        //将验证码图片以流的方式响应到浏览器
        ImageIO.write(code.getImage(), "png", response.getOutputStream());
    }
}
